package com.example.plan.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.plan.common.result.Result;
import com.example.plan.model.dto.user.UserLoginRequest;
import com.example.plan.model.dto.user.UserQueryRequest;
import com.example.plan.model.dto.user.UserRegisterRequest;
import com.example.plan.model.dto.user.UserUpdateRequest;
import com.example.plan.model.entity.User;
import com.example.plan.exception.ErrorCode;
import com.example.plan.exception.ThrowUtils;
import com.example.plan.model.vo.UserVo;
import com.example.plan.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.List;

@Slf4j
@Api(tags = "用户模块")
@RestController
@RequestMapping("users")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户注册
     *
     * @param userRegisterRequest 用户注册信息
     * @return success
     */
    @ApiOperation(value = "用户注册")
    @PostMapping("/register")
    public Result<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        log.debug("用户注册", userRegisterRequest);
        ThrowUtils.throwIf(userRegisterRequest == null, ErrorCode.PARAMS_ERROR);
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        return userService.userRegister(userAccount, userPassword, checkPassword);
    }

    /**
     * 用户登录
     *
     * @param userLoginRequest 用户登录信息
     * @param request          http请求体
     * @return success
     */
    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public Result<User> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        log.debug("用户登录", userLoginRequest);
        ThrowUtils.throwIf(userLoginRequest == null, ErrorCode.PARAMS_ERROR);
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        return userService.userLogin(userAccount, userPassword, request);

    }

    /**
     * 用户登出
     *
     * @param request http请求体
     * @return success
     */
    @ApiOperation(value = "用户登出")
    @PostMapping("/layout")
    public Result<String> layout(HttpServletRequest request) {
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR);
        return userService.logout(request);
    }

    /**
     * 获取当前登录用户信息
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "获取当前登录用户信息")
    @GetMapping("/getCurrentUser")
    public Result<User> getUserInfo(HttpServletRequest request) {
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR);
        User user = userService.getUserInfo(request);
        user.setPassword_hash(null);
        return Result.success(user);
    }


    /**
     * 查询数据库所有用户
     *
     * @param request
     * @return 数据库所用用户
     */
    @ApiOperation(value = "查询数据库所有用户")
    @GetMapping("/getUserList")
    public Result<Page<UserVo>> getUserList(HttpServletRequest request) {
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR);
        return userService.getAllUsers(request);
    }

    /**
     * 条件分页查询用户
     *
     * @param userQueryRequest 用户查询条件
     * @return BaseResponse<Page < UserVo>> 用户列表
     */
    @ApiOperation(value = "条件分页查询用户")
    @PostMapping("/getUserByConditions")
    public Result<Page<UserVo>> getUserByConditions(@RequestBody UserQueryRequest userQueryRequest) {
        log.debug("条件分页查询用户", userQueryRequest);
        ThrowUtils.throwIf(userQueryRequest == null, ErrorCode.PARAMS_ERROR);
        return userService.getUsersByConditions(userQueryRequest);
    }

    /**
     * @param userUpdateRequest 用户更新信息
     * @return ???
     */
    @ApiOperation(value = "更新用户信息")
    @PutMapping("/updateUser")
    public Result<UserVo> UpdateUser(@RequestBody UserUpdateRequest userUpdateRequest) {
        log.debug("更新用户信息", userUpdateRequest);
        ThrowUtils.throwIf(userUpdateRequest == null, ErrorCode.PARAMS_ERROR);
        return userService.updateUser(userUpdateRequest);
    }

    /**
     * 更新用户密码
     *
     * @param userId      用户ID
     * @param oldPassword 旧密码
     * @param newPassword 新密码
     * @return 更新结果
     */
    @ApiOperation(value = "更新用户密码")
    @PutMapping("/updatePassword")
    public Result<Boolean> updateUserPassword(@RequestParam Long userId, @RequestParam String oldPassword, @RequestParam String newPassword) {
        log.debug("更新用户密码: userId={}, oldPassword={}, newPassword={}", userId, oldPassword, newPassword);
        ThrowUtils.throwIf(userId == null || StrUtil.hasBlank(oldPassword, newPassword), ErrorCode.PARAMS_ERROR);
        return userService.updateUserPassword(userId, oldPassword, newPassword);
    }

    @ApiOperation(value = "软删除用户")
    @DeleteMapping("/deleteUser/{id}")
    public Result<Boolean> deleteUser(@PathVariable Long id) {
        log.debug("删除用户:", id);
        ThrowUtils.throwIf(id == null, ErrorCode.PARAMS_ERROR);
        return userService.deleteUser(id);
    }

    /**
     * 重置用户密码
     *
     * @param userId      用户ID
     * @param newPassword 新密码
     * @return 更新结果
     */
    @ApiOperation(value = "重置用户密码")
    @PutMapping("/resetPassword")
    public Result<Boolean> resetUserPassword(@RequestParam Long userId, @RequestParam String newPassword) {
        log.debug("重置用户密码: userId={}, newPassword={}", userId, newPassword);
        ThrowUtils.throwIf(userId == null || StrUtil.hasBlank(newPassword), ErrorCode.PARAMS_ERROR);
        return userService.resetUserPassword(userId, newPassword);
    }


    /**
     * 导出用户数据
     *
     * @return 用户数据列表
     */
    @ApiOperation(value = "导出用户数据")
    @GetMapping("/exportUsers")
    public Result<List<UserVo>> exportUsers() {
        return userService.exportUsers();
    }

    /**
     * 导入用户数据
     *
     * @param users 用户数据列表
     * @return 导入结果
     */
    @ApiOperation(value = "导入用户数据")
    @PostMapping("/importUsers")
    public Result<Boolean> importUsers(@RequestBody List<User> users) {
        log.debug("导入用户数据: users={}", users);
        ThrowUtils.throwIf(CollUtil.isEmpty(users), ErrorCode.PARAMS_ERROR);
        return userService.importUsers(users);
    }
}
